package com.hmall.api.config;

import com.hmall.api.fallback.CartClientFallback;
import com.hmall.api.fallback.ItemClientFallback;
import com.hmall.api.fallback.PayClientFallback;
import com.hmall.api.fallback.TradeClientFallback;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {

    //注册pay-service的fallback
    @Bean
    public PayClientFallback payClientFallback() {
        return new PayClientFallback();
    }


    //注册trade-service的fallback
    @Bean
    public TradeClientFallback tradeClientFallback() {
        return new TradeClientFallback();
    }

    //注册关于购物车远程调用的客户端的fallback
    @Bean
    public CartClientFallback cartClientFallback() {
        return new CartClientFallback();
    }

    //注册关于商品远程调用客户端的fallback
    @Bean
    public ItemClientFallback itemClientFallback() {
        return new ItemClientFallback();
    }

    @Bean
    public Logger.Level feignLogLevel() {
        return Logger.Level.FULL;
    }

    //feign请求拦截器
    @Bean
    public RequestInterceptor feignRequestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                //获取线程变量中的用户id
                Long userId = UserContext.getUser();
                //设置到feign请求头中
                if (userId != null) {
                    requestTemplate.header("user-info", userId.toString());
                }
            }
        };
    }


}
